package com.hongsheng.controller;

/*
@Author：郭振文
@Package：com.hongsheng.controller
@File：RoleController
@Date：2023/8/10 23:48
*/

import com.github.pagehelper.PageInfo;
import com.hongsheng.pojo.*;
import com.hongsheng.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    //添加角色
    @PostMapping("/insert")
    public R insertRole(@RequestBody Role role){
        if (role.getRoleName()==null || role.getRoleName().isEmpty()){
            return new R(false);
        }else if(role.getDepartment()==null || role.getDepartment().isEmpty()) {
            return new R(false);
        } else {
            Boolean flag = roleService.insertRole(role);
            return new R(flag);
        }
    }

    //删除角色
    @DeleteMapping("{id}")
    public R deleteRole(@PathVariable Integer id){
        Boolean flag = roleService.deleteRole(id);
        return new R(flag);
    }

    //通过id查询角色
    @GetMapping("{id}")
    public R selectRoleById(@PathVariable Integer id){
        Role role = roleService.selectRoleById(id);
        return new R(true,role);
    }

    //修改角色
    @PutMapping
    public R updateRole(@RequestBody Role role) {
        if (role.getRoleName() == null || role.getRoleName().isEmpty()) {
            return new R(false);
        } else if (role.getDepartment() == null || role.getDepartment().isEmpty()) {
            return new R(false);
        } else {
            Boolean flag = roleService.updateRole(role);
            return new R(flag);
        }
    }

    //分页条件查询角色
    @GetMapping("{currentPage}/{pageSize}")
    public R selectRole(@PathVariable Integer currentPage, @PathVariable Integer pageSize, Role role){
        PageInfo<Role> roleList = roleService.selectRole(currentPage,pageSize,role);
        return new R(true,roleList);
    }

    //添加角色权限
    @PostMapping
    public R insertRoleAuthority(@RequestBody Role role) {
        if (role.getAid() == null || role.getAid().isEmpty()) {
            return new R(false);
        }
        List<Integer> aidList = role.getAid();
        Boolean flag = true;
        for (int i = 0; i < aidList.size(); i++) {
            Integer rid = role.getId();
            Integer aid = aidList.get(i);
            RA ra = new RA();
            ra.setRid(rid);
            ra.setAid(aid);
            Boolean flag1 = roleService.selectRA(ra);
            if (flag1) {
                flag = roleService.insertRoleAuthority(ra);
                if (flag == false) {
                    break;
                }
            }else {
                return new R(false,"添加权限不能重复");
            }
        }
        return new R(flag);
    }

    //删除角色权限
    @PostMapping("/delete")
    public R deleteRoleAuthority(@RequestBody Role role){
        if (role.getAid()== null || role.getAid().isEmpty()){
            return new R(false);
        }
        List<Integer> aidList = role.getAid();
        Boolean flag = true;
        for (int i = 0; i < aidList.size(); i++) {
            Integer rid = role.getId();
            Integer aid = aidList.get(i);
            RA ra = new RA();
            ra.setRid(rid);
            ra.setAid(aid);
            Boolean flag1 = roleService.selectRA(ra);
            if (!flag1) {
                flag = roleService.deleteRoleAuthority(ra);
                if (flag == false) {
                    break;
                }
            } else {
                return new R(false, "删除权限不存在");
            }
        }
        return new R(flag);
    }

    //查询所有角色
    @GetMapping
    public R selectRole1(){
        List<Role> roleList = roleService.selectRole1();
        return new R(true,roleList);
    }





}
